"""
This file is created to get stats about a table 
"""

import xml.etree.cElementTree as ET
import MySQLdb
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

#"""
site_file ="temp_sites"# "41_sites_name"#"all_site_name"
table_type1 = "posts"
migration_table = "qimp_migration_124_41_aug13"
insert_table = "question_stat"
file_path = "D:\Research\ISEC-2014\data\content"
database = "isec2014"
user = "root"
password="123"
port=3306

"""
site_file = "41_sites_name"
table_type1 = "posts"
migration_table = "qimp_migration_124_41_aug13"
insert_table = "question_stat"
file_path = "E:\Sangeeta\dataset\\2013_05_stack_overflow\Content"
database = "sangeeta"
user = "sangeetal"
password="sangeetal"
port=3307
#"""

db1= MySQLdb.connect(host="localhost", user=user, passwd=password,db=database, port=port)
select_cursor = db1.cursor() 
insert_cursor = db1.cursor() 

file = open(site_file)
for line in file:
    #site = line.strip() +".com"
    #print  "site=",site
    table  = table_type1+"_"+line.strip()+"_aug13"
    
    print "table=", table
    str1 = "select count(*) from `" +table+"` where posttypeid=1"
    str2 = "select count(*) from `"+migration_table +"` where migrated= '"+line.strip()+"'"
    print "str1=", str1
    print "str2", str2    
    
    site_name = line.strip()
    select_cursor.execute(str1)
    temp1 = select_cursor.fetchall()
    question_count =(str)(temp1[0][0])
    
    select_cursor.execute(str2)
    temp2 = select_cursor.fetchall()
    q_from_other_sites = (str)(temp2[0][0])
    
  
    
    print "val =" , temp1[0][0], ",", temp2[0][0]
    
    insert_str = "insert into `"+insert_table+"` values('"+site_name+"',"+question_count+","+q_from_other_sites+")"
    print "insert=", insert_str
    insert_cursor.execute(insert_str)



db1.commit()
